package com.qifan.energy.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.StringUtils;
import com.qifan.energy.base.BaseActivity;
import com.qifan.energy.bean.vo.UpdatePwdVO;
import com.qifan.energy.R;
import com.qifan.energy.http.net.NetUtils;
import com.qifan.energy.http.response.MyObserver;
import com.qifan.energy.util.ActivityStackUtil;
import com.qifan.energy.util.ToastUtils;
import com.qifan.energy.util.count.CountDownTimerUtil;
import com.qifan.energy.widget.dialog.LoadingUtils;
import com.orhanobut.logger.Logger;

import butterknife.BindView;
import butterknife.OnClick;

public class ForgetPwdActivity extends BaseActivity {

    /*@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd);
    }*/

    @BindView(R.id.done_btn)
    TextView doneBtn;
    @BindView(R.id.phone_et)
    EditText phoneEt;
    @BindView(R.id.code_et)
    EditText codeEt;
    @BindView(R.id.pwd_et)
    EditText pwdEt;
    @BindView(R.id.pwd2_et)
    EditText pwd2Et;

    private boolean isPwdVisible = false;
    private boolean isPwd2Visible = false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forget_pwd;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        /* 更改状态栏 */
        changeStatusTransparentFontBlack();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.code_tv)
    void onCodeTvClicked(TextView tv){
        String phone = phoneEt.getText().toString();
        if (TextUtils.isEmpty(phone)){
            ToastUtils.showShort("手机号不能为空");
            return;
        } else {
            if (phone.length() != 11){
                ToastUtils.showShort("手机号格式不对哦~");
                return;
            }
            tv.setEnabled(false);
            LoadingUtils.show(this);
            NetUtils.getAuthCode(phone, new MyObserver<String>(this) {
                @Override
                public void onSuccess(String result) {
                    tv.setEnabled(true);
                    LoadingUtils.hide();
                    Logger.i("sso/getAuthCode:" + result);

                    /* 开始倒计时 */
                    CountDownTimerUtil mCountDownTimer = new CountDownTimerUtil(tv, 60000, 1000);
                    mCountDownTimer.start();
                }

                @Override
                public void onFailure(Throwable e, String errorMsg) {
                    tv.setEnabled(true);
                    LoadingUtils.hide();
                    ToastUtils.showShort(errorMsg);
                }
            });
        }
    }

    @OnClick(R.id.done_btn)
    void onDoneBtnClicked(){
        String phone = phoneEt.getText().toString();
        String code = codeEt.getText().toString();
        String pwd = pwdEt.getText().toString();
        String pwd2 = pwd2Et.getText().toString();
        if (StringUtils.isTrimEmpty(phone)){
            ToastUtils.showShort("手机号不能为空");
            return;
        }
        if (StringUtils.isTrimEmpty(code)){
            ToastUtils.showShort("验证码不能为空");
            return;
        }
        if (StringUtils.isTrimEmpty(pwd)){
            ToastUtils.showShort("密码不能为空");
            return;
        }
        if (StringUtils.isTrimEmpty(pwd2)){
            ToastUtils.showShort("请输入确认密码");
            return;
        }
        //比较两次输入是否一致
        if (!StringUtils.equalsIgnoreCase(pwd.trim(),pwd2.trim())){
            ToastUtils.showShort("两次输入密码不一致");
            return;
        }
        doneBtn.setEnabled(false);
        LoadingUtils.show(this);
        UpdatePwdVO forgetVO = new UpdatePwdVO();
        forgetVO.setTelephone(phone);
        forgetVO.setAuthCode(code);
        forgetVO.setPassword(pwd);
        NetUtils.updatePwd(forgetVO, new MyObserver<String>(this) {
            @Override
            public void onSuccess(String result) {
                doneBtn.setEnabled(true);
                LoadingUtils.hide();
                //提示并跳转
                ToastUtils.showShort("修改成功!");
                ActivityStackUtil.finishTopActivity();
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                doneBtn.setEnabled(true);
                LoadingUtils.hide();
                ToastUtils.showShort(errorMsg);
            }
        });
    }

    @OnClick(R.id.password_eye_iv)
    void onPasswordEyeIvClicked(ImageView imageView){
        isPwdVisible = !isPwdVisible;
        if (isPwdVisible){
            imageView.setImageResource(R.mipmap.ic_edit_eye_open_gray);
            pwdEt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            imageView.setImageResource(R.mipmap.ic_edit_eye_close_gray);
            pwdEt.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
        pwdEt.setSelection(pwdEt.getText().length());
    }

    @OnClick(R.id.password_eye_iv2)
    void onPassword2EyeIvClicked(ImageView imageView){
        isPwd2Visible = !isPwd2Visible;
        if (isPwd2Visible){
            imageView.setImageResource(R.mipmap.ic_edit_eye_open_gray);
            pwd2Et.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            imageView.setImageResource(R.mipmap.ic_edit_eye_close_gray);
            pwd2Et.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
        pwd2Et.setSelection(pwd2Et.getText().length());
    }
}
